package com.water.service.Impl;

import com.water.context.BaseContext;
import com.water.entity.Meter;
import com.water.entity.User;
import com.water.exception.ParameterException;
import com.water.mapper.MeterMapper;
import com.water.mapper.UserMapper;
import com.water.service.MeterService;
import com.water.service.UserService;
import com.water.vo.MeterVo;
import com.water.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: city_water
 * @description:
 * @author: lydms
 * @create: 2024-05-11 15:56
 **/

@Service
public class MeterServiceImpl implements MeterService {


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MeterMapper meterMapper;

    /**
     * 获取水表信息
     *
     * @return
     */

    public MeterVo getInfo() {

        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //判断用户id是否为空
        if (userId == null) {
            throw new ParameterException("用户未登录");
        }
        //获取用户信息
        User user = userMapper.selectByUserId(userId);

        //跟据用户id查询水表信息
        Meter meter = meterMapper.selectByUserId(userId);
        MeterVo meterVo = new MeterVo();
        //将实体类中的属性值复制到vo中
        BeanUtils.copyProperties(meter, meterVo);
        meterVo.setUserName(user.getName());

        return meterVo;
    }
}
